#coding=utf-8
"""
===========================
Author:杭州多测师_周sir
Time:2023/3/24 14:03
website:https://duoceshi.net
===========================
"""
'''
此模块是封装所有用例的基类
比如：所有用例要用到的元素定位方法，以及输入框的点击，下拉等等公共方法
'''
import sys
sys.path.append('..')
import unittest
from time import *

# #调试代码
# from selenium import  webdriver
# driver = webdriver.Chrome()

class BasePage(unittest.TestCase):  #创建一个BasePage类，这个类继承了unittest.TestCase类

    @classmethod
    def set_driver(cls,driver):  #入参是driver对象，把创建好的driver对象传进来，变成BasePage类的属性
        cls.driver = driver

    @classmethod
    def get_driver(cls):   #单例设计模式
        return cls.driver

    # baidu_input('id','kw')
    @classmethod  #封装元素定位
    def find_element(cls,elenment):
        type = elenment[0]  #id
        value = elenment[1] #kw
        if type == "id":
            elem = cls.driver.find_element_by_id(value)
        elif type == "xpath":
            elem = cls.driver.find_element_by_xpath(value)
        elif type == "class":
            elem = cls.driver.find_element_by_class_name(value)
        elif type == 'name':
            elem = cls.driver.find_element_by_name(value)
        elif type == 'css':
            elem = cls.driver.find_element_by_css_selector(value)
        elif type == 'link_text':
            elem = cls.driver.find_element_by_link_text(value)
        elif type == "partial":
            elem = cls.driver.find_element_by_partial_link_text(value)

        else:  #如果不是上面的定位方式则手动触发异常：请输入正确的参数
            raise  ValueError('plese input corrt paramters')

        return elem
    @classmethod  #封装输入函数
    def sendKeys(cls,elem,text):
        return elem.send_keys(text)

    @classmethod  #封装点击操作
    def click(cls,elem):
        return elem.click()
    @classmethod #封装隐式等待
    def wailt(cls,sec):
        return cls.driver.implicitly_wait(sec)
    @classmethod
    def frame(cls,elem):
        '''切换iframe框'''
        return cls.driver.switch_to.frame(elem)

    @classmethod
    def outframe(cls):
        '''退出iframe'''
        return cls.driver.switch_to.default_content()

    @classmethod
    def sleep(cls,sec):
        return sleep(sec)
    @classmethod
    def get_text(cls,element):
        '''封装根据网页元素拿到text值'''
        value = BasePage.find_element(element).text
        return value
    @classmethod
    def refresh(cls):
        cls.driver.refresh()


if __name__ == '__main__':
    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    driver.maximize_window()
    baidu_input =('id','kw')
    elem = BasePage.find_element(baidu_input)
    BasePage.sendKeys(elem,'多测师')
    baidu_click = ('id','su')
    elem =BasePage.find_element(baidu_click)
    elem.click()













































